package com.thinkfit.jy.activity;

import android.content.Context;
import android.media.AudioManager;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.common.lib.activity.BaseActivity;
import com.common.lib.bean.UserBean;
import com.common.lib.constant.EventBusEvent;
import com.common.lib.fragment.BaseFragment;
import com.common.lib.manager.DataManager;
import com.common.lib.mvp.contract.EmptyContract;
import com.common.lib.mvp.presenter.EmptyPresenter;
import com.common.lib.utils.BaseUtils;
import com.thinkfit.jy.R;
import com.thinkfit.jy.fragment.UserProfileFragment;
import com.thinkfit.jy.utils.DialogViewUtils;
import com.thinkfit.jy.utils.SimpleSettingViewUtils;
import com.thinkfit.jy.utils.SportsStatusBarViewUtils;
import com.thinkfit.jy.utils.ThemeUtils;
import com.thinkfit.jy.utils.Utils;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 个人中心
 */
public class UserProfileActivity extends BaseActivity<EmptyContract.Presenter> implements EmptyContract.View {

    ConstraintLayout clMain;
    TextView tvArea,tvLanguage,tvUnit;;
    private ArrayList<BaseFragment> mFragments;
    private int mCurrentItem;

    @Override
    protected int getLayoutId() {
//        return DataManager.Companion.getInstance().getMainStyle() == 3 ? R.layout.activity_user_profile_xm : R.layout.activity_user_profile;
        return R.layout.activity_user_info;
    }

    @Override
    protected void onCreated(@Nullable Bundle savedInstanceState) {
//        WindowManager.LayoutParams params = getWindow().getAttributes();
//        params.height = getDisplayMetrics().heightPixels;
//        getWindow().setAttributes(params);
//        int currentStyle = DataManager.Companion.getInstance().getMainStyle();
//        if (currentStyle == 3) {
//            Bitmap bmp = (Bitmap) DataManager.Companion.getInstance().getObject();
//            Bitmap newBmp = Bitmap.createBitmap(bmp, 0, 0, getDisplayMetrics().widthPixels, getDisplayMetrics().heightPixels);
//            setBackground(R.id.fl, newBmp);
//        }
//        params.gravity = Gravity.LEFT;
//        getWindow().setBackgroundDrawableResource(android.R.color.transparent);
//        showUserInfo();
        setViewsOnClickListener(R.id.cl_set_item,R.id.tv0, R.id.tv1, R.id.tv2, R.id.ivLogout,R.id.ll_logout,R.id.ll_sport_back);
        clMain = findViewById(R.id.cl_main);

        tvArea = findViewById(R.id.tv_area);
        tvLanguage = findViewById(R.id.tv_language);
        tvUnit = findViewById(R.id.tv_unit);

        mCurrentItem = 0;
        mFragments = new ArrayList<>();
        mFragments.add(UserProfileFragment.getInstance(0));
        mFragments.add(UserProfileFragment.getInstance(1));
        mFragments.add(UserProfileFragment.getInstance(2));
        ViewPager viewPager = findViewById(R.id.viewPager);
        viewPager.setOffscreenPageLimit(3);
        viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @NonNull
            @Override
            public Fragment getItem(int position) {
                return mFragments.get(position);
            }

            @Override
            public int getCount() {
                return mFragments.size();
            }

        });
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                mCurrentItem = position;
                resetBtn();
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        viewPager.setCurrentItem(mCurrentItem);

        Utils.initRegion(tvArea, tvLanguage,tvUnit);
    }

    @Override
    protected void onResume() {
        super.onResume();
        SportsStatusBarViewUtils.getInstance().isMain(false);
        clMain.setBackgroundResource(ThemeUtils.getInstance().getMainTheme());
        initSound();
    }

    @Override
    public void onReceive(@NonNull HashMap<String, Object> map) {
        if (map == null) {
            return;
        }
        super.onReceive(map);
        if(map.containsKey(EventBusEvent.VOICE_CHANGE)){
            int i = (int) map.get(EventBusEvent.VOICE_CHANGE);
            if(i <= 0){
                setImage(R.id.iv_sound,R.drawable.img_simple_set_sound_off);

            }else {
                setImage(R.id.iv_sound,R.drawable.img_simple_set_sound_max);
            }
//            SportsStatusBarViewUtils.getInstance().setVolume((int) map.get(EventBusEvent.VOICE_CHANGE));
        }
    }

    public void initSound(){
        AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        int currentVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
        if(currentVolume <= 0){
            setImage(R.id.iv_sound,R.drawable.img_simple_set_sound_off);

        }else {
            setImage(R.id.iv_sound,R.drawable.img_simple_set_sound_max);
        }
    }

    @NonNull
    @Override
    protected EmptyContract.Presenter onCreatePresenter() {
        return new EmptyPresenter(this);
    }

    private void showUserInfo() {
        UserBean myInfo = DataManager.Companion.getInstance().getMyInfo();
        if (myInfo == null) {
            return;
        }
        setText(R.id.tvName, myInfo.getNickname());
        BaseUtils.StaticParams.loadImage(this, R.drawable.app_not_login, myInfo.getHeadimg(), findViewById(R.id.ivAvatar));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.cl_set_item:
                SimpleSettingViewUtils.getInstance().initView(getContext(),this);
                break;
            case R.id.ivLogout:
            case R.id.ll_logout:
//                Intent intent = new Intent(this, DialogActivity.class);
//                intent.putExtra(Constants.BUNDLE_EXTRA, DialogActivity.SHOW_LOGOUT);
//                startActivity(intent);
                DialogViewUtils.getInstance().initView(getContext(),false,DialogViewUtils.EXIT_LOGOUT);
                break;
            case R.id.tv0:
                ViewPager viewPager = findViewById(R.id.viewPager);
                viewPager.setCurrentItem(0);
                break;
            case R.id.tv1:
                viewPager = findViewById(R.id.viewPager);
                viewPager.setCurrentItem(1);
                break;
            case R.id.tv2:
                viewPager = findViewById(R.id.viewPager);
                viewPager.setCurrentItem(2);
                break;
            case R.id.ll_sport_back:
                onBackPressed();
                break;
        }
    }

    private void resetBtn() {
        for (int i = 0; i < 3; ++i) {
            int id = getResources().getIdentifier("tv" + i, "id", getPackageName());
            TextView tv = findViewById(id);
            if (i == mCurrentItem) {
                tv.setBackgroundResource(R.drawable.shape_gradient_18_d0b085_a87c42_360);
                tv.setTextColor(ContextCompat.getColor(this, com.common.lib.R.color.white));
            } else {
                tv.setBackground(null);
                tv.setTextColor(ContextCompat.getColor(this, com.common.lib.R.color.text_color_1));
            }
        }
    }
}